package PMCXMLParser;


import java.util.Properties;
import java.io.File;

public class Parse_PMC_XML
{
    File_Manager_Impl fileManager = new File_Manager_Impl();
    private final String EXPRESSIONS_TO_BE_REMOVED[] =
    {
        "http://dbpedia.org/resource/"
    };
    private final String TAGS_TO_REPLACE_WITH_NEWLINE[] =
    {
        "<p>", "</p>"
    };
    private final String TAGS_TO_REPLACE_WITH_SPACE[] =
    {
       
      "<underline>","</underline>","<bold>","</bold>","<italic>","</italic>","<title>","</title>", "</p>", "</sec>", "<sup>", "</sup>", "<sub>", "</sub>",
        "   "
    };
    
    String_Manipulator_Impl strMan = new String_Manipulator_Impl();
    Properties props;

    public Parse_PMC_XML()
    {
       
    }

    /**
     * Converts the XML files of PubMedCentral XML format to text
     */
     public void convert_pmc_xml_to_text()
    {
        
        String CORPUS_XML="E:\\Disease\\"; 
        String CORPUS_TEXT="E:\\DiseaseTXT\\"; 
                
        File dir = new File("E:\\Disease\\");
        String[] str = dir.list();

        File xmlFile;

        for(String inputFileName : str)
        {
            System.out.println("Parsing "+inputFileName );
            xmlFile = new File(CORPUS_XML + inputFileName);
            String outputFileName = CORPUS_TEXT+xmlFile.getName();
            StringBuffer contents = new StringBuffer();
            StringBuffer parsed_contents = new StringBuffer();
            contents =fileManager.fileReader(xmlFile);
            String trimmed_text = contents.toString();
            trimmed_text = remove_references(trimmed_text);
            parsed_contents.append(trimmed_text);
            fileManager.fileWriter(parsed_contents,  outputFileName);
        }

    }
   

    /*
     * It removes the expressions from the text to clean it. The removed expressions include
     * the equations, tables etc.
     */
    public String remove_references(String str)
    {

        for(String patternStr : EXPRESSIONS_TO_BE_REMOVED)
        {
              
            str = strMan.pattern_replacer(str, patternStr, "");
        

        }
        for(String patternStr : TAGS_TO_REPLACE_WITH_SPACE)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }
        return str;
    }

    /*
     * 
     */
    public String extract_heading(String str)
    {
        for(String patternStr : TAGS_TO_REPLACE_WITH_NEWLINE)
        {
            str = strMan.pattern_replacer(str, patternStr, "\n");
        }

        for(String patternStr : TAGS_TO_REPLACE_WITH_SPACE)
        {
            str = strMan.pattern_replacer(str, patternStr, "");
        }

                  
        str = strMan.string_replacer_start_zero(str, "<inline-formula>", "</inline-formula>", "");   
        str = strMan.string_replacer_start_zero(str, "<m:math", "</m:math>", "");
        str = strMan.string_replacer_start_zero(str, "<graphic file", "/>", "");
        str = strMan.string_replacer_start_zero(str, "<fig id=", "</fig>", "");
        str = strMan.string_replacer_start_zero(str, "<tbl id=", "</tbl>", "");
        str = strMan.string_replacer_start_zero(str, "<suppl id=", "</suppl>", "");
        str = strMan.string_replacer_start_zero(str, "<xref ref-type=", "</xref>", "");
        str = strMan.string_replacer_start_zero(str, "<disp-formula", "</disp-formula>", "");
        str = strMan.string_replacer_start_zero(str, "<supplementary-material", "</supplementary-material>", "");
        str = strMan.string_replacer_start_zero(str, "<table-wrap", "</table-wrap>", "");
        str = strMan.string_replacer_start_zero(str,"<ext-link ext-link-type=", ">", "");
        str = strMan.string_replacer_start_zero(str,"<fig", "fig>", "");
        
       

        return str;
    }
}
